#ifndef IPCAM_Export_H_
#define IPCAM_Export_H_

#ifdef __HuaweiLite__
#include "app_common.h"
#include <liteos/sysinfo.h>
//#include <liteos/if_arp.h>
#include "netif/ifaddrs.h"
#include "lwip/sockets.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/igmp.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcpip.h"
#include "lwip/pbuf.h"
#include "lwip/netif.h"
#include "lwip/ip_addr.h"
#include "lwip/dhcp.h"
#include "lwip/filter.h"

#include "DevManager.h"
#endif

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <stdint.h>
#include <errno.h>
#include <dirent.h> 
#include <netdb.h> 
#include <semaphore.h>

//#include <linux/un.h>
#ifdef PLATFORM_hisi3518
#include <linux/videodev.h>
#endif
//#include <linux/fb.h>
//#include <linux/un.h>
//#include <linux/soundcard.h>

#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
//#include <sys/ipc.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
//#include <sys/sysinfo.h>
#include <arpa/inet.h>
#include <netinet/in.h> 
#include <netinet/tcp.h>
#include <sys/vfs.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>



#define 	TRUE 1
#define 	FALSE 0
#define 	TIMEOUT_SEC             (10)
#define 	TIMEOUT_USEC            (0)

#define	    ERR_UNKNOW			    			(0xFFFF)
#define     ERR_STREAM_NODATA                   (0xff01)
#define	    ERR_NO_ERROR						(0x0001)

#define 	INT int
#define 	CHAR char
#define 	USHORT unsigned short
#define 	VOID  void
#define 	UINT unsigned int
#define 	UCHAR unsigned char
#define 	ULONG unsigned long
#define 	BOOL  int
#define     __start_code__              ((USHORT)0xE0AF)
#define     MSG_SEARCHQUERY_REQ                 0x2001      //设备搜索
#define     MSG_SEARCHQUERY_RESP                0x2002      //设备搜素响应
#define     MSG_DEVREGIST_REQ                   0x2003      //设备注册
#define     MSG_DEVREGIST_RESP                  0x2004      //设备注册响应

#define	    MSG_FACTORYINFOSET_REQ			    0x2005		//厂家信息设置
#define	    MSG_FACTORYINFOSET_RESP				0x2006		//厂家信息设置回应
#define     MSG_FACTORYINFOQUARY_REQ            0x2007
#define     MSG_FACTORYINFOQUARY_RESP           0x2008      //
#define 	MSG_USERLOGIN_REQ		    		0x0101		//用户登陆请求
#define 	MSG_USERLOGIN_RESP			        0x0102		//用户登陆回应	

#define 	MSG_NETPARMSET_REQ			        0x0C01		//网络参数设置请求	
#define 	MSG_NETPARMSET_RESP			        0x0C02		//网络参数设置回应
#define 	MSG_NETPARMQUERY_REQ		        0x0C03		//网络参数查询请求	
#define 	MSG_NETPARMQUERY_RESP		        0x0C04		//网络参数查询回应

#define 	MSG_DEVKEYSET_REQ			        0x0C09		//key设置请求	
#define 	MSG_DEVKEYSET_RESP			        0x0C0A		//key设置回应
#define 	MSG_DEVKEYQUERY_REQ		            0x0C0B		//key查询请求	
#define 	MSG_DEVKEYQUERY_RESP		        0x0C0C		//key查询回应

#define     MSG_CONNECT_UPDATE             		0XFF07       //update

#define		MSG_UPDATE_REQ						0x2101		//升级云台协议
#define		MSG_UPDATE_RESP						0x2102		//升级应用程序

#define 	MSG_LOADCFG_REQ						0x0E07		//恢复出厂设置
#define 	MSG_LOADCFG_RESP					0x0E08

#define 	MSG_REBOOT_REQ						0x0E03		//设备重启请求		CGI
#define 	MSG_REBOOT_RESP						0x0E04		//设备重启回应		CGI
#define 	MSG_AGING_REQ						0x0F05		//硬件老化程序请求
#define 	MSG_AGING_RESP						0x0F06		//硬件老化程序回应

#define     MSG_DEVICEOPER_CMD_REQ              0x2009      //设备命令的操作

#define    MSG_DEVICEOPER_CMD_RESP				0X200A  	//设备命令响应回应

#define     ERR_NOW_UPGRADE                     0x0101       //SD upgrade



typedef struct{
	UCHAR			ipFetchMode;	       //静态IP或者动态DHCP分配IP
	UCHAR			ipAddr    [24];		   //IP 地址
	UCHAR			gwAddr    [24];		   //网关
	UCHAR			netMask   [24];		   //子网掩码
	UCHAR			macAddr   [24];		   //MAC 地址
	UCHAR           NetDnsSvI [16];        //DNS 
	UCHAR           NetDnsSvII[16];
	UCHAR           HostName  [16];        //主机名
}T_IPCNETPARAM;

typedef struct{
	INT					iServerPort;
	INT					ipFetchMode;	       //静态IP或者动态DHCP分配IP				
	UCHAR				NetIpAddr[16];				//IP 地址
	UCHAR				NetGwAddr[16];				//gateway
	UCHAR				NetSubMask[16];				//net mask
	UCHAR				NetMacAddr[24];				//MAC addr
	UCHAR				NetDnsSvI [16];				//DNS1
	UCHAR				NetDnsSvII[16];				//DNS2

	UCHAR				devModel[20];               //出厂型号
	UCHAR				devSerialNumb[64];          //出厂编号
	UCHAR				devRegcode[26];             //设备注册码
	CHAR				devName[24];                //设备显示名称
	CHAR				szFactoryName[32];			//厂家名称

	CHAR                nVideoChannel;              //视频通道数
	CHAR                bIsVideoSubChannel;         //是否有视频子码流
	CHAR                nAudioChannel;              //音频通道数
	
	CHAR 				DevSerachToolVersions;		//设备搜索工具版本，放在最后面兼容 新搜索工具 旧ipcam
	UCHAR				LogoType;					//选用哪个LOGO 用于厂商信息设置
	ULONG				FactoryTime;				//出厂时间。   用于厂商信息设置
	CHAR 				HardWareVersion[32];
	CHAR 				SoftWareVersion[32];
	CHAR				sensor[32];

	INT					WifiOFF;					//是否支持wifi
	INT					SDOFF;						//是否支持sd
	INT                 POEOFF;						//是否支持sdk
	INT					AudioInOFF;					//是否支持audio input
	INT					AudioOutOFF;				//是否支持audio output 
	INT					IOOFF;						//是否支持IO 
	INT					ThreeMachineSupport;		//是否支持三倍机
	CHAR 				PTPUUID[32];           	    //设备号 
	ULONG       		Uptime;                     //运行时间
	ULONG       		FreeMem;					//剩余内存
	INT                 WifiValue;                  //信号强度
	CHAR                PTPCMEI[32];                //yd cmei 有效15
	//CHAR				revcmd[32];                 //接收命令的字符串 

}T_DEVSEARCHINFO;
//544 - sizeof (T_DEVSEARCHINFO);
typedef enum{
	UPDATE_TYPE_BOOT    =0X01, //启动
	UPDATE_TYPE_LINUX   =0X02, //内核
	UPDATE_TYPE_ROOTFS  =0X03, //文件系统
	UPDATE_TYPE_APP     =0X04, //运用镜像
	UPDATE_TYPE_PTZ     =0X05, //PTZ脚本

}ENU_UPDATETYPE;
typedef struct{
	INT                 nFileType;                  //文件类型 (ENU_UPDATETYPE)
	INT					iFileLen;                   //文件长度
	CHAR				szUpdateFileName[128];      //文件名称(板上存储名称，非全路径)

}T_DEVUPDATE;

typedef struct 
{
	UCHAR       		devModel[20];               //出厂型号
	UCHAR				devSerialNumb[64];          //出厂编号
    UCHAR       		devRegcode[26];             //设备注册码
	CHAR				devName[24];                //设备显示名称
	CHAR				szFactoryName[32];			//厂家名称
}T_IPCFACTORYPARAM;

typedef struct {                      //包头
	USHORT     sync;
	USHORT	   msgType;
	USHORT     subType;
	USHORT	   len;
	USHORT	   errCode;
    USHORT     reserve;
}T_MSGHEAD; 
typedef struct {
	T_MSGHEAD  head;
	CHAR       msgbuf[532];
}T_MSGBUF;


typedef struct{
	T_MSGHEAD		    head;
    T_IPCNETPARAM       netparam;
}IPCAM_NET_PARAM_SET_REQ,  IPCAM_NET_PARAM_SET_RESP,
 IPCAM_NET_PARAM_QUARY_REQ,IPCAM_NET_PARAM_QUARY_RESP;

typedef struct{				//设备搜索
	T_MSGHEAD			head;
	T_DEVSEARCHINFO		DevSearchInfo;
}IPCAM_DEV_SEARCH_REG, IPCAM_DEV_SEARCH_RESP;

typedef struct{
	T_MSGHEAD			head;
	T_DEVUPDATE			DevUpdate;
}IPCAM_DEV_UPDATE_REG, IPCAM_DEV_UPDATE_RESP;
typedef struct{
	T_MSGHEAD			head;
	T_IPCFACTORYPARAM	FactoryParam;
}IPCAM_FACTORY_PARAM_REQ;


typedef struct {                     
	char DeviceKey[76];
}T_IPCDEVKEY; 

typedef struct{
	T_MSGHEAD			head;
	T_IPCDEVKEY         DeviceKey;
}IPCAM_DEVKEY_PARAM_REQ;

typedef struct {
	T_MSGHEAD head;
	char	  DevMacAddr[24];
	char 	  cmd[544 - 24 - sizeof(T_MSGHEAD)];
}IPCAM_CMD;

typedef union{
	
	T_MSGHEAD		               MsgHead;
	T_MSGBUF                       MsgBuf;
	//厂家设定 SET
	IPCAM_FACTORY_PARAM_REQ		   MsgFactoryParam;
	
	//NET PARAM SET
	IPCAM_NET_PARAM_SET_REQ	       MsgNetParam;


	//设备搜索
	IPCAM_DEV_SEARCH_REG		   MsgDevSearch;
	//设备升级
	IPCAM_DEV_UPDATE_REG		   MsgDevUpdate;

	//设备命令	
	IPCAM_CMD					   MsgDevCmd;

	IPCAM_DEVKEY_PARAM_REQ         MsgDevKey;
}NETMSGPACKET,*LPNETMSGPACKET;

#define  	SIZEOFNETMSGHEAD	         (sizeof(T_MSGHEAD))
#ifdef __HuaweiLite__
#define     SIZEOFNETMSGPACK             (0)  
#else
#define     SIZEOFNETMSGPACK             (sizeof(NETMSGPACKET))  
#endif /* __HuaweiLite__ */


#define	IPCAM_PTHREAD_DETACH			do{ pthread_detach(pthread_self()); }while(0)
#define IPCAM_DEBUG_WARN(msg...) do{printf("\033[1;33m [daemon-W] %s:%s:%d \033[0m:",__FILE__,  __FUNCTION__,  __LINE__);printf(msg);}while(0)
#define IPCAM_DEBUG(msg...) printf("[ %s, %d ]=> ",__FILE__,  __LINE__);printf(msg);printf("\r\n")
#define	IPCAM_CREATE_THREADEX(Func, Args,Ret)	do{					\
		pthread_t		__pth__;									\
		if (0 == pthread_create(&__pth__, NULL, (void *)Func, (void *)Args))	\
			Ret = TRUE; \
		else \
			Ret = FALSE; \
      }while(0)


#endif
